/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: task_manager_proxy.cpp
 */

#include <boost/asio.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>

#include "task_manager_proxy.hpp"

using namespace middleware;

//-----------------------------------------------------------------------------
// Constructors and destructor
/* Default private constructor */
TaskManagerProxy::TaskManagerProxy() {
	this->_config.ip_bind_man = "127.0.0.1"; /* Not used */
	this->_config.ip_to_man = "127.0.0.1";
	this->_config.port_bind_man = "0";
	this->_config.port_to_man = "0";
}
/* Private parameter constructor */
TaskManagerProxy::TaskManagerProxy(ManProxyConfig conf) {
	this->_config.ip_bind_man = conf.ip_bind_man; /* Not used */
	this->_config.ip_to_man = conf.ip_to_man;
	this->_config.port_bind_man = conf.port_bind_man;
	this->_config.port_to_man = conf.port_to_man;
}
/* Destructor */
TaskManagerProxy::~TaskManagerProxy() {

}
//-----------------------------------------------------------------------------
// Public members
/* Sending task */
void TaskManagerProxy::send_task(const TaskDescriptor& task)
		throw(ProxyException) {
	using boost::asio::ip::tcp;
	// Managing destination unavailable
	for (int i = 10; true; i--) {
		try {
			// Sending a task descriptor
			tcp::iostream td_stream(this->_config.ip_to_man,
					this->_config.port_to_man); /* Creating TCP stream */
			if (!td_stream) {
				// Problem in sending...
				throw ProxyException("Unable to open TCP stream!");
			} else {
				// OK we can send it
				boost::archive::binary_oarchive oa(td_stream);
				oa << task; /* Sending workflow --> SENT */
				td_stream.close();
				return; /* Get away */
			}
		} catch(ProxyException& e) {
			// Could not reach destination: try again
			if (i <= 0) throw ProxyException(std::string(
					"Error in proxy: Unreachable destination: ") + e.what());
			boost::this_thread::sleep(boost::posix_time::milliseconds(100));
		} catch(std::exception& e) {
			// Unknown exception
			throw ProxyException(
					std::string("Not reversible error in proxy: ") + e.what());
		}
	}
}
/* Receiving task */
TaskDescriptor TaskManagerProxy::recv_task() throw(ProxyException) {
	using boost::asio::ip::tcp;
	int port = boost::lexical_cast<int>(this->_config.port_bind_man);
	boost::asio::io_service io_s;
	try {
		tcp::acceptor td_acceptor(io_s, tcp::endpoint(tcp::v4(), port));
		tcp::iostream td_stream;
		TaskDescriptor td;
		td_acceptor.accept(*(td_stream.rdbuf())); /* Accepting */
		boost::archive::binary_iarchive ia(td_stream);
		ia >> td; /* Here I have the workflow */
		td_stream.close();
		return td;
	} catch(std::exception& e) {
		throw ProxyException(std::string("Error in proxy: ") + e.what());
	}
}
